package com.tjsinfo.tjsoa.system.dao;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.tjsinfo.tjsoa.system.vo.TjsRoles;

/**
 * A data access object (DAO) providing persistence and search support for
 * TjsRoles entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see com.tjsinfo.tjsoa.system.vo.TjsRoles
 * @author MyEclipse Persistence Tools
 */

public class TjsRolesDAO extends HibernateDaoSupport implements ITjsRolesDAO {
	private static final Log log = LogFactory.getLog(TjsRolesDAO.class);
	// property constants
	public static final String NAME = "name";
	public static final String ROLE_ID = "roleId";
	public static final String URL = "url";
	public static final String PARENT_ID = "parentId";
	public static final String ORDER_NO = "orderNo";
	public static final String IS_MENU = "isMenu";
	public static final String IMG_URL = "imgUrl";
	public static final String TARGET = "target";

	protected void initDao() {
		// do nothing
	}

	public int save(TjsRoles transientInstance) {
		log.debug("saving TjsRoles instance");
		int id=0;
		try {
			getHibernateTemplate().save(transientInstance);
			id=transientInstance.getId();
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
		return id;
	}

	public void delete(TjsRoles persistentInstance) {
		log.debug("deleting TjsRoles instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public TjsRoles findById(java.lang.Integer id) {
		log.debug("getting TjsRoles instance with id: " + id);
		try {
			TjsRoles instance = (TjsRoles) getHibernateTemplate().get(
					"com.tjsinfo.tjsoa.system.vo.TjsRoles", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(TjsRoles instance) {
		log.debug("finding TjsRoles instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding TjsRoles instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from TjsRoles as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}
	
	public List findByName(Object name) {
		return findByProperty(NAME, name);
	}

	public List findByRoleId(Object roleId) {
		return findByProperty(ROLE_ID, roleId);
	}

	public List findByUrl(Object url) {
		return findByProperty(URL, url);
	}

	public List findByParentId(Object parentId) {
		return findByProperty(PARENT_ID, parentId);
	}

	public List findByOrderNo(Object orderNo) {
		return findByProperty(ORDER_NO, orderNo);
	}

	public List findByIsMenu(Object isMenu) {
		return findByProperty(IS_MENU, isMenu);
	}

	public List findByImgUrl(Object imgUrl) {
		return findByProperty(IMG_URL, imgUrl);
	}

	public List findByTarget(Object target) {
		return findByProperty(TARGET, target);
	}

	public List findAll() {
		log.debug("finding all TjsRoles instances");
		try {
			String queryString = "from TjsRoles";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public TjsRoles merge(TjsRoles detachedInstance) {
		log.debug("merging TjsRoles instance");
		try {
			TjsRoles result = (TjsRoles) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(TjsRoles instance) {
		log.debug("attaching dirty TjsRoles instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(TjsRoles instance) {
		log.debug("attaching clean TjsRoles instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static TjsRolesDAO getFromApplicationContext(ApplicationContext ctx) {
		return (TjsRolesDAO) ctx.getBean("TjsRolesDAO");
	}
}